﻿Imports System
Imports System.Windows
Imports System.Windows.Forms
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.SqlServer.Management.IntegrationServices
Imports Microsoft.SqlServer.Management.Smo

Public Class frmSSISCatalogNavigator

    Public sFullSSISPkgPath As String

    Private Sub btnConnect_Click(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) Handles btnConnect.Click
        ' connect to the server indicated in the txtServer textbox
        ' hook into the SSISDB catalog
        ' build out the SSISDB node by iterating the objects stored therein
        ' load the node and display it

        With Me
            Dim oServer As New Server(.txtServer.Text)
            Dim oIS As New IntegrationServices(oServer)
            Dim cat As Catalog = oIS.Catalogs("SSISDB")
            Dim L1Node As New TreeNode("SSISDB")
            L1Node.ImageIndex = 0
            Dim L2Node As TreeNode
            Dim L3Node As TreeNode
            Dim L4Node As TreeNode

            For Each f As CatalogFolder In cat.Folders
                L2Node = New TreeNode(f.Name)
                L2Node.ImageIndex = 1
                L1Node.Nodes.Add(L2Node)
                '.tvCatalog.Nodes.Add(L2Node)
                For Each pr As ProjectInfo In f.Projects
                    L3Node = New TreeNode(pr.Name)
                    L3Node.ImageIndex = 2
                    L2Node.Nodes.Add(L3Node)
                    '.tvCatalog.Nodes.Add(L3Node)
                    For Each pkg As Microsoft.SqlServer.Management.IntegrationServices.PackageInfo In pr.Packages
                        L4Node = New TreeNode(pkg.Name)
                        L4Node.ImageIndex = 3
                        L3Node.Nodes.Add(L4Node)
                        '.tvCatalog.Nodes.Add(L4Node)
                    Next
                Next
            Next

            .tvCatalog.Nodes.Add(L1Node)
        End With

    End Sub

    Private Sub btnSelect_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) Handles btnSelect.Click
        btnSelectClick()

    End Sub

    Private Sub btnSelectClick()
        ' if the image index level indicates a package,
        ' select this node, populate the sFullSSISPkgPath variable,
        ' and close the form

        With Me
            If Not .tvCatalog.SelectedNode Is Nothing Then
                If .tvCatalog.SelectedNode.ImageIndex = 3 Then
                    sFullSSISPkgPath = .txtServer.Text & "." & .tvCatalog.SelectedNode.FullPath()
                    .Close()
                End If
            End If
        End With
    End Sub
    Private Sub tvCatalog_DoubleClick(ByVal sender As Object, _
                                      ByVal e As System.EventArgs) Handles tvCatalog.DoubleClick
        ' run the Select Click logic

        With Me
            btnSelectClick()
        End With

    End Sub

    Private Sub frmSSISCatalogNavigator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' initialize and load frmISTree

        With Me
            .Text = "Integration Services Catalog Navigator"
            .txtServer.Text = "localhost"
            .Show()
        End With

    End Sub
End Class